package com.lsl.controller;

import com.lsl.dto.UserDTO;
import com.lsl.entity.FollowingUser;
import com.lsl.entity.Result;
import com.lsl.service.FollowingUserService;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/following-service")
public class FollowingController {

    @Autowired
    private FollowingUserService followingUserService;

    @PostMapping("/follow")
    public Result<Boolean> followUser(@RequestBody FollowingUser followingUser) {
        if (followingUser.getUserId() == null || followingUser.getFollowingUserId() == null) {
            return Result.error(400, "用户 ID 或被关注的用户 ID 获取失败");
        }
        boolean success = followingUserService.followUser(
            followingUser.getUserId(),
            followingUser.getFollowingUserId()
        );
        return success ? Result.success(true) : Result.error(500, "关注失败");
    }

    @DeleteMapping("/unfollow/{userId}/{followingUserId}")
    public Result<Boolean> unfollowUser(
        @PathVariable Integer userId, 
        @PathVariable Integer followingUserId) {
        boolean success = followingUserService.unfollowUser(userId, followingUserId);
        return success ? Result.success(true) : Result.error(500, "取消关注失败");
    }

    @GetMapping("/check/{userId}/{followingUserId}")
    public Result<Boolean> isFollowing(
            @PathVariable Integer userId,
            @PathVariable Integer followingUserId) {
        boolean isFollowing = followingUserService.isFollowing(userId, followingUserId);
        return Result.success(isFollowing);
    }

    @GetMapping("/getFollowingUsers/{userId}")
    public Result<List<UserDTO>> getFollowingUsers(@PathVariable Integer userId) {
        List<UserDTO> followingUsers = followingUserService.getFollowingUsers(userId);
        return Result.success(followingUsers);
    }

    @GetMapping("/getFollowingMe/{userId}")
    public Result<List<UserDTO>> getFollowers(@PathVariable Integer userId) {
        List<UserDTO> followers = followingUserService.getFollowers(userId);
        return Result.success(followers);
    }

}
